package vn.student.mylib;

import java.util.ArrayList;
import vn.student.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListClassAdapter extends ArrayAdapter<Class>{
	Activity act;
	public ArrayList<Class> list;
	boolean isHaveAddNewBtn = true;
	Class classInfo;
	public ListClassAdapter(Activity act,ArrayList<Class> list,boolean isHaveAddNewBtn) {
		super(act, R.layout.class_row,list);
		this.act=act;
		this.list = list;
		this.isHaveAddNewBtn = isHaveAddNewBtn;
	}
	public View getView(int position, View convertView,ViewGroup parent) {
		 View row=convertView;
		 RowWrapper wrapper=null;
	     if (row==null) {                         
	    	 LayoutInflater inflater=act.getLayoutInflater();
	        
	    	 row=inflater.inflate(R.layout.class_row, null);
	    	 wrapper=new RowWrapper(row);
	    	 row.setTag(wrapper);
	     }
	     else {
	    	 wrapper=(RowWrapper)row.getTag();
	     }
	     classInfo = list.get(position);
	     wrapper.getTypeLabel().setText(ClassType.getName(classInfo.type));
	     wrapper.getClassNameLabel().setText(classInfo.className);
	     wrapper.getlocationLabel().setText(classInfo.location);
	     wrapper.getTimeLabel().setText(classInfo.timeStart+"-"+ classInfo.timeEnd);
	     wrapper.getAssCountLabel().setText(classInfo.assCount+" Assignement");
	     wrapper.getIcon().setImageResource(ClassType.getIconId(classInfo.type));
	     if(classInfo.repeat.equals("0")){
				setNoneRepeatView(row);
			}
			else
				if(classInfo.repeat.equals("1")){
					setRepeatWeeksView(row);
				}
				else{
					setRepeatWorkingDaysView(row);
				}
	     if(list.size()==1){
			 if(isHaveAddNewBtn)
				 row.setBackgroundResource(R.drawable.class_row_bottom);
			 else
				 row.setBackgroundResource(R.drawable.class_row_all);
		 }
		 else{
			 if(position==(list.size()-1)){
				 row.setBackgroundResource(R.drawable.class_row_bottom);
			 }
			 else 
				 if(position==0&&(!isHaveAddNewBtn))
					 row.setBackgroundResource(R.drawable.class_row_top);
				 else
					 row.setBackgroundResource(R.drawable.class_row_none);
		 }
	     return(row);
	}
	private void setRepeatWeeksView(View row){ 
		RelativeLayout rl = (RelativeLayout) row.findViewById(R.id.day);
		rl.removeAllViews();
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.week_day_mini, rl); 
		int rlId = WeekDay.getDayId(classInfo.dateRepeat);
		rl = (RelativeLayout) row.findViewById(rlId);
		rl.setBackgroundResource(R.drawable.week_day_2);
	}
	private void setRepeatWorkingDaysView(View row){
		RelativeLayout rl = (RelativeLayout) row.findViewById(R.id.day);
		rl.removeAllViews();
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.week_day_mini, rl); 
		for(int i=1;i<WeekDay.num.length-1;i++){
			rl = (RelativeLayout) row.findViewById(WeekDay.num[i]);
			rl.setBackgroundResource(R.drawable.week_day_2);
		}
	}
	private void setNoneRepeatView(View row){
		RelativeLayout rl = (RelativeLayout) row.findViewById(R.id.day);
		rl.removeAllViews();
		TextView tmp1 = new TextView(act);
		tmp1.setText(classInfo.dateStart);
		rl.addView(tmp1);
	}
	public ArrayList<Class> getClassInfoList(){
		return list;
	}
	public void setClassInfoList(ArrayList<Class> list){
		this.list = list;
	}
	
	private class RowWrapper {
		View base;
		TextView classNameLabel = null;
		TextView timeLabel=null;
		TextView locationLabel=null;
		TextView assCountLabel = null;
		TextView typeLabel;
		ImageView classIcon = null;
		
		ImageView getIcon() {
			if (classIcon==null) {
				classIcon=(ImageView)base.findViewById(R.id.classIcon);
			}
			return(classIcon);
		}
	  
		RowWrapper(View base) {
			this.base=base;
		}
		TextView getTypeLabel() {
			if (typeLabel==null) {
				typeLabel=(TextView)base.findViewById(R.id.typeLabel);
			}
		  return(typeLabel);
		}
		TextView getClassNameLabel() {
			if (classNameLabel==null) {
				classNameLabel=(TextView)base.findViewById(R.id.classNameLabel);
			}
		  return(classNameLabel);
		}
		TextView getTimeLabel() {
			if (timeLabel==null) {
				timeLabel=(TextView)base.findViewById(R.id.timeLabel);
			}
		  return(timeLabel);
		}
		TextView getlocationLabel() {
			if (locationLabel==null) {
				locationLabel=(TextView)base.findViewById(R.id.locationLabel);
			}
		  return(locationLabel);
		}
		TextView getAssCountLabel() {
			if (assCountLabel==null) {
				assCountLabel=(TextView)base.findViewById(R.id.assCountLabel);
			}
		  return(assCountLabel);
		}
	}
}
